/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package docking;

import java.io.*;
import java.util.ArrayList;

/**
 *
 * @author Simin
 */
public class DataHandler implements Serializable {
    private int reservationNumber;
    private ArrayList<Boat> boatList;
    private ArrayList<Reservation> reservationList;
    private ArrayList<Pier> pierList;
    private ArrayList<Service> serviceList;
    
    public DataHandler()
    {
        boatList = new ArrayList<Boat>();
        reservationList = new ArrayList<Reservation>();
        pierList = new ArrayList<Pier>();
        serviceList = new ArrayList<Service>();
        reservationNumber = 1;
        testData();
    }
    
    /**
     * Metoda sum loadar testData
     */
    private void testData()
    {
        Pier h1 = new Pier("H1", 100, 10, 90);
        Pier h2 = new Pier("H2", 100, 10, 80);
        Pier h3 = new Pier("H3", 100, 20, 270);
        addPier(h1);
        addPier(h2);
        addPier(h3);
        
        Boat b1 = new Boat("Mascot", "448553", 9, 20);
        Boat b2 = new Boat("Stapin", "261933", 10, 25);
        addBoat(b1);
        addBoat(b2);
        
        Reservation r1 = new Reservation(b1, 2012, 3, 14, 3, 2012, 3, 15, 5, generateReservationNumber());
        Reservation r2 = new Reservation(b1, 2012, 5, 17, 9, 2012, 5, 17, 10, generateReservationNumber());
        
        r1.setPier(h1);
        r1.setPier(h2);
        
        b1.addReservation(r1);
        b1.addReservation(r2);
        
                
        addReservation(r1);
        addReservation(r2);
        
        h1.addReservation(r1);
        h1.addReservation(r2);
        
        Service s1 = new Service("Oil", 10000, "Refills oil tank");
        Service s2 = new Service("ShipWash", 900, "Bikini Babes will wash your Ship");
        
        addService(s1);
        addService(s2);
        
    }
    
    
    public void saveSerialization()
    {
        DataHandler data = new DataHandler();
        data.setBoatList(boatList);
        data.setReservationList(reservationList);
        data.setPierList(pierList);
        data.setServiceList(serviceList);
        data.setReservationNumber(reservationNumber);
        
        
        try
        {
            FileOutputStream fileOut =
            new FileOutputStream("data.ser");
            ObjectOutputStream out =
                            new ObjectOutputStream(fileOut);
            out.writeObject(data);
            out.close();
            fileOut.close();
         }catch(IOException i)
         {
            System.out.println("FAIL TO SAVE!!!");
         }
    }
    
    public void loadSerialization()
    {
         DataHandler e = null;
         try
         {
            FileInputStream fileIn =
                          new FileInputStream("data.ser");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            e = (DataHandler) in.readObject();
            in.close();
            fileIn.close();
         }catch(IOException i)
         {
             i.printStackTrace();
             return;
         }catch(ClassNotFoundException c)
         {
             System.out.println("FAIL TO LOAD DATA");
         }
    }
    
    /**
     * Geneatar ein teljari sum telur upp hvørjaferð hon verður kalla
     * hon skal geva eitt nummar til nyggjar resevatiónir
     * 
     * @return int ResevationNummbar
     */
    public int generateReservationNumber()
    {
        int temp = reservationNumber;
        reservationNumber++;
        return temp;
    }
            
    /**
     * Returnerar ein ArrayLista av Øllum Boats sum eru í okkara systemi
     * 
     * 
     * @return ArrayList<Boat> of Boats
     */
    public ArrayList<Boat> getBoats()
    {
        ArrayList<Boat> temp = new ArrayList<Boat>();
        
        for(Boat b :  getBoatList())
        {
            temp.add(b);
        }
        return temp;
    }
    
    /**
     * Addar ein Boat til okkara System
     * @param Boat
     */
    public void addBoat(Boat b)
    {
        getBoatList().add(b);
    }
    
    /**
     * gevur ein ArrayList av Øllum resevations í okkara system
     * @return ArrayList<Resevation> Resevations in system
     */
    public ArrayList<Reservation> getReservations()
    {
        ArrayList<Reservation> temp = new ArrayList<Reservation>();
        
        for(Reservation r : getReservationList())
        {
            temp.add(r);
        }
        return temp;
    }
    
    /**
     * Addar Resevation til okkara System
     * @param Resevation
     */
    public void addReservation (Reservation r)
    {
        getReservationList().add(r);
        //calender.addReservation(r);
    }
    
    
    
    /**
     * Returnerar ein Pier úr okkara lista av Pier´s
     * 
     * 
     * @param number
     * @return Pier
     */
    public Pier getPier(int number)
    {
        return getPierList().get(number);
    }
    
    /**
     * Tekur eina reservation og returner pier som hevur reservatiónina
     * 
     * @param Reservation
     * @return Pier that contains Reservation
     */
    public Pier getPier(Reservation r)
    {
        for(Pier p : getPierList())
        {
            if(p.getReservationList().contains(r))
            {
                return p;
            }
        }
        return null;
    }
    
    /**
     * addar Pier til okkara Pier lista
     * 
     * @param Pier
     */
    public void addPier(Pier p)
    {
        getPierList().add(p);
    }
    
    /**
     * gevur ArrayList av Øllum Piers í okkara System
     * 
     * @return 
     */
    public ArrayList<Pier> getPiers()
    {
        return getPierList();
    }
    
    
    
    
    /**
     * addar Service til okkara arraylist av Services
     * @param Service
     */
    public void addService(Service s)
    {
        serviceList.add(s);
    }
    
    /**
     * returnerar ArrayList av services sum eru í system
     * 
     * @return 
     */
    public ArrayList<Service> getServiceList()
    {
        return serviceList;
    }
    
    /**
     * Returnar ein arrayList av øllum Reservations sum liggja
     *          við dock hendan dagin
     * 
     * @param year
     * @param month
     * @param day
     * @return ArrayList<Reservation> tann dagin
     */
    public ArrayList<Reservation> getReservations(int year, int month, int day)
    {
        
        ArrayList<Reservation> temp = new ArrayList<Reservation>();
        for(Reservation r : getReservationList())
        {
            if(r.getFromYear() <= year )
            {
                if(r.getFromMonth()<= month)
                {
                    if(r.getFromDay() <= day)
                    {
                        if(r.getToYear() >= year)
                        {
                            if(r.getToMonth() >= month)
                            {
                                if(r.getToDay() >= day)
                                {
                                    temp.add(r);
                                }
                            }
                        }
                    }
                }
            }
        }
        return temp;
    }

    /**
     * @param reservationNumber the reservationNumber to set
     */
    public void setReservationNumber(int reservationNumber) {
        this.reservationNumber = reservationNumber;
    }

    /**
     * @return the boatList
     */
    public ArrayList<Boat> getBoatList() {
        return boatList;
    }

    /**
     * @param boatList the boatList to set
     */
    public void setBoatList(ArrayList<Boat> boatList) {
        this.boatList = boatList;
    }

    /**
     * @return the reservationList
     */
    public ArrayList<Reservation> getReservationList() {
        return reservationList;
    }

    /**
     * @param reservationList the reservationList to set
     */
    public void setReservationList(ArrayList<Reservation> reservationList) {
        this.reservationList = reservationList;
    }

    /**
     * @return the pierList
     */
    public ArrayList<Pier> getPierList() {
        return pierList;
    }

    /**
     * @param pierList the pierList to set
     */
    public void setPierList(ArrayList<Pier> pierList) {
        this.pierList = pierList;
    }

    /**
     * @param serviceList the serviceList to set
     */
    public void setServiceList(ArrayList<Service> serviceList) {
        this.serviceList = serviceList;
    }
            
    
    
    
}
